package laser.juliette.simulator.extension.edcare;

import java.io.Serializable;
import java.lang.reflect.Method;

import laser.juliette.ams.AMSException;
import laser.juliette.ams.AgendaItem;
import laser.juliette.ams.UnknownParameter;
import laser.juliette.simulator.clock.ClockConfigurator;
import laser.juliette.simulator.extension.SimulationTrace;

public class EdcareSimulationTrace implements SimulationTrace {
	
	private static final int runId = getRunId();
	
	public void completed(AgendaItem arg0) throws AMSException {
		/*
		for(String parameter : arg0.parameterNames()) {
			System.out.print(parameter + "; ");
			try {
				System.out.println(arg0.getParameter(parameter));
			} catch (UnknownParameter e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		//*/

		/*
		try {
			System.out.println(arg0.getParameter("patientInfo"));
		} catch (UnknownParameter e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//*/
		
		showAgendaItem(arg0);
	}

	public void posted(AgendaItem arg0) throws AMSException {
		showAgendaItem(arg0);
	}
	
	public void started(AgendaItem arg0) throws AMSException {
		showAgendaItem(arg0);
	}
	
	public void starting(AgendaItem arg0) throws AMSException {
		showAgendaItem(arg0);
	}
	
	public void completing(AgendaItem arg0) throws AMSException {
		showAgendaItem(arg0);
	}
	
	public void optedout(AgendaItem arg0) throws AMSException {
		showAgendaItem(arg0);
	}
	
	public void terminated(AgendaItem arg0) throws AMSException {
		showAgendaItem(arg0);
	}
	
	public void terminating(AgendaItem arg0) throws AMSException {
		showAgendaItem(arg0);
	}
	
	
	private void showAgendaItem(AgendaItem item) throws AMSException {
		/*
		try {
			Method m;
			Serializable pInfoParam = item.getParameter("patientInfo");
			
			m = pInfoParam.getClass().getMethod("getAllInfo", new Class[0]);
			System.out.println(m.invoke(pInfoParam, new Object[0]));
		} catch(Exception e) {}
		if(true) {
			return;
		}
		//*/
		
		/*
		try {
			Method m;
			Serializable pInfoParam = item.getParameter("patientInfo");
			
			m = pInfoParam.getClass().getMethod("getPatientNo", new Class[0]);
			if((Integer)m.invoke(pInfoParam, new Object[0]) != 3) {
				//return;
			}
		} catch(Exception e) {}
		//*/
		
		System.out.println("--->");
		//System.out.println("patientId: " + getPatientId(item));
		
		/*
		System.out.print(item.getStep().getName());
		System.out.print(getID(item));
		System.out.print(" was assigned to ");
		System.out.print(item.getAgenda().getName());
		System.out.print(" at ");
		System.out.println(ClockConfigurator.getClock().getCurrentTime());
		//*/
		
		System.out.println("State: " + item.getState());
		System.out.println("Time: " + ClockConfigurator.getClock().getCurrentTime());
		System.out.println("Step: " + item.getStep().getName());
		System.out.println("Resource ID: " + item.getAgenda().getName());
		
		try {
			Method m;
			Serializable pInfoParam = item.getParameter("patientInfo");

			System.out.println("Resource type: patient");
			
			m = pInfoParam.getClass().getMethod("getPatientNo", new Class[0]);
			System.out.println("Patient id: " + m.invoke(pInfoParam, new Object[0]));
			
			m = pInfoParam.getClass().getMethod("getTriageAcuity", new Class[0]);
			System.out.println("Acuity: " + m.invoke(pInfoParam, new Object[0]));
		} catch(Exception e) {}
		
		try {
			Method m;
			Serializable infoParam = item.getParameter("agent");
			
			m = infoParam.getClass().getMethod("isA", String.class);
			if((Boolean)m.invoke(infoParam, "doctor")) {
				System.out.println("XResource type: doctor");
			}
			else if((Boolean)m.invoke(infoParam, "nurse")) {
				System.out.println("XResource type: nurse");
			}
			else if((Boolean)m.invoke(infoParam, "anybed")) {
				System.out.println("XResource type: bed");
			}
			else {
				System.out.println("Resource type: unknown");
			}
		} catch(Exception ex) {
			ex.printStackTrace();
		}
		System.out.println("---");
		
	}

	private String getID(AgendaItem item) {
		//return ((laser.juliette.simulator.ams.AgendaItem)item).id;
		//return (new StringBuilder(" (")).append(((laser.juliette.simulator.ams.AgendaItem)item).id).append(")").toString();
		String s = " (";
		s += ((laser.juliette.simulator.ams.AgendaItem)item).id;
		s += ")>";
		
		return s;
		
		//
		//return Integer.toString((((laser.juliette.simulator.ams.AgendaItem)item).id));
	}
	
	// This is a hack to get the run id.  It has potential concurrency issues.
	private static int getRunId() {
		// TODO
		return 1;
	}

}